package main

import (
	"context"
	"github.com/go-redis/redis/v8"
	"log"
	"renting-demo/model/mysql"
	"strconv"
	"time"
)

var st *redis.Client

// 脚本更新虚拟库存
func main() {
	for {
		goodsId, err := st.BRPop(context.Background(), 0, "goodsid").Result()
		if err != nil {
			log.Println(err)
		}
		if goodsId != nil {
			num, _ := st.Get(context.Background(), "vnum "+goodsId[1]).Result()
			nums, _ := strconv.Atoi(num)
			for i := 0; i < nums; i++ {
				st.LPush(context.Background(), "Virtual Inventory", goodsId[1])
			}
		}
		if time.Now().Unix() == this() {
			mysql.UpdateMysql(goodsId[1])
		}
	}

}

func this() int64 {
	now := time.Now()                                                   // 获取当前时间
	nextDay := now.Add(24 * time.Hour)                                  // 获取第二天的时间
	year, month, day := nextDay.Date()                                  // 获取第二天日期
	zeroTime := time.Date(year, month, day, 0, 0, 0, 0, now.Location()) // 设置为凌晨0点
	timestamp := zeroTime.Unix()                                        // 获取第二天凌晨0点的时间戳

	return timestamp
}
